package com.fr.view.core;

import com.fr.base.background.ColorBackground;
import com.fr.base.core.GraphHelper;
import com.fr.report.PaperSize;
import com.fr.report.Report;
import com.fr.report.ReportPage;
import com.fr.report.ReportSettings;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import com.fr.report.painter.PagePainter;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D.Double;
import javax.swing.JPanel;
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.UIManager;

public class InnerPreviewPane extends JPanel
  implements Scrollable
{
  private ReportPage reportPage;
  private boolean adjustMargin = false;
  private int dragType = DEFAULT;
  private int dragXY = -1;
  private int columnIndex = -1;
  public static int DEFAULT = 0;
  public static int MARGIN_TOP = 1;
  public static int MARGIN_LEFT = 2;
  public static int MARGIN_BOTTOM = 3;
  public static int MARGIN_RIGHT = 4;
  public static int COLUMN = 5;

  public InnerPreviewPane()
  {
    setBackground(new Color(128, 128, 128));
    setOpaque(false);
  }

  public boolean isAdjustMargin()
  {
    return this.adjustMargin;
  }

  public void setAdjustMargin(boolean paramBoolean)
  {
    this.adjustMargin = paramBoolean;
  }

  public int getDragType()
  {
    return this.dragType;
  }

  public void setDragType(int paramInt)
  {
    this.dragType = paramInt;
  }

  public int getDragXY()
  {
    return this.dragXY;
  }

  public void setDragXY(int paramInt)
  {
    this.dragXY = paramInt;
  }

  public int getColumnIndex()
  {
    return this.columnIndex;
  }

  public void setColumnIndex(int paramInt)
  {
    this.columnIndex = paramInt;
  }

  public void print(ReportPage paramReportPage)
  {
    this.reportPage = paramReportPage;
    getParent().validate();
    getParent().doLayout();
    repaint(10L);
  }

  public void paintComponent(Graphics paramGraphics)
  {
    int i7;
    super.paintComponent(paramGraphics);
    if (this.reportPage == null)
      return;
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    Dimension localDimension = getSize();
    ReportSettings localReportSettings = this.reportPage.getReportSettings();
    int i = this.reportPage.getPageWidth();
    int j = this.reportPage.getPageHeight();
    int k = this.reportPage.getMarginTop();
    int l = this.reportPage.getMarginLeft();
    int i1 = this.reportPage.getMarginBottom();
    int i2 = this.reportPage.getMarginRight();
    localGraphics2D.translate((localDimension.getWidth() - i) / 2.0D, 8.0D);
    Report localReport = this.reportPage.getSourceReport();
    int i3 = 0;
    int i4 = 0;
    double d1 = 0.0D;
    double d2 = 0.0D;
    if (localReportSettings.isLonelyPage())
    {
      DynamicValueList localDynamicValueList1 = ReportHelper.getColumnWidthList(localReport);
      DynamicValueList localDynamicValueList2 = ReportHelper.getRowHeightList(localReport);
      int i5 = localReport.getColumnCount();
      int i6 = localReport.getRowCount();
      for (i7 = 0; i7 < i5; ++i7)
        i3 += localDynamicValueList1.get(i7);
      for (i7 = 0; i7 < i6; ++i7)
        i4 += localDynamicValueList2.get(i7);
      d1 = i / (l + i2 + i3);
      d2 = j / (i4 + k + i1);
      d1 = (d1 < 1.0D) ? d1 : 1.0D;
      d2 = (d2 < 1.0D) ? d2 : 1.0D;
      localGraphics2D.scale(d1, d2);
      ColorBackground.getInstance(Color.white).paint(localGraphics2D, new Rectangle2D.Double(0.0D, 0.0D, l + i2 + i3, i4 + k + i1));
    }
    new PagePainter(this.reportPage, localGraphics2D, false).convert();
    localGraphics2D.setPaint(UIManager.getColor("controlDkShadow"));
    if (localReportSettings.isLonelyPage())
      GraphHelper.drawRect(localGraphics2D, 0.0D, 0.0D, (d1 == 1.0D) ? i : l + i2 + i3, (d2 == 1.0D) ? j : i4 + k + i1);
    else
      GraphHelper.drawRect(localGraphics2D, 0.0D, 0.0D, i, j);
    if (isAdjustMargin())
    {
      localGraphics2D.setPaint(Color.DARK_GRAY);
      GraphHelper.drawLine(localGraphics2D, 0.0D, k, i, k, 7);
      GraphHelper.drawLine(localGraphics2D, 0.0D, j - i1, i, j - i1, 7);
      GraphHelper.drawLine(localGraphics2D, l, 0.0D, l, j, 7);
      GraphHelper.drawLine(localGraphics2D, i - i2, 0.0D, i - i2, j, 7);
      localGraphics2D.setPaint(Color.BLACK);
      double d3 = 1.5D;
      double d4 = 8.0D;
      GraphHelper.fillRect(localGraphics2D, 0.0D, k - d3, d4, d3 * 2.0D + 1.0D);
      GraphHelper.fillRect(localGraphics2D, i - d4, k - d3, d4, d3 * 2.0D + 1.0D);
      GraphHelper.fillRect(localGraphics2D, l - d3, 0.0D, d3 * 2.0D + 1.0D, d4);
      GraphHelper.fillRect(localGraphics2D, l - d3, j - d4, d3 * 2.0D + 1.0D, d4);
      GraphHelper.fillRect(localGraphics2D, 0.0D, j - i1 - d3, d4, d3 * 2.0D + 1.0D);
      GraphHelper.fillRect(localGraphics2D, i - d4, j - i1 - d3, d4, d3 * 2.0D + 1.0D);
      GraphHelper.fillRect(localGraphics2D, i - i2 - d3, 0.0D, d3 * 2.0D + 1.0D, d4);
      GraphHelper.fillRect(localGraphics2D, i - i2 - d3, j - d4, d3 * 2.0D + 1.0D, d4);
      i7 = l;
      for (int i8 = 0; i8 < this.reportPage.getColumnCount(); ++i8)
      {
        i7 += this.reportPage.getColumnWidth(i8);
        GraphHelper.fillRect(localGraphics2D, i7 - d3, 0.0D, d3 * 2.0D + 1.0D, d4);
        GraphHelper.drawLine(localGraphics2D, i7, 0.0D, i7, d4 * 3.0D / 2.0D);
      }
      if (getDragType() != DEFAULT)
      {
        localGraphics2D.setPaint(Color.DARK_GRAY);
        if ((this.dragType == MARGIN_LEFT) || (this.dragType == MARGIN_RIGHT) || (this.dragType == COLUMN))
          GraphHelper.drawLine(localGraphics2D, getDragXY(), 0.0D, getDragXY(), j, 7);
        else if ((this.dragType == MARGIN_TOP) || (this.dragType == MARGIN_BOTTOM))
          GraphHelper.drawLine(localGraphics2D, 0.0D, getDragXY(), i, getDragXY(), 7);
      }
    }
  }

  public Dimension getPreferredSize()
  {
    if (this.reportPage == null)
      return new Dimension((int)PaperSize.PAPERSIZE_A4.getWidth() * 96, (int)PaperSize.PAPERSIZE_A4.getHeight() * 96);
    return new Dimension(this.reportPage.getPageWidth(), this.reportPage.getPageHeight() + 16);
  }

  public Dimension getPreferredScrollableViewportSize()
  {
    return getPreferredSize();
  }

  public int getScrollableUnitIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2)
  {
    switch (paramInt1)
    {
    case 1:
      return (paramRectangle.height / 10);
    case 0:
      return (paramRectangle.width / 10);
    }
    throw new IllegalArgumentException("Invalid orientation: " + paramInt1);
  }

  public int getScrollableBlockIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2)
  {
    switch (paramInt1)
    {
    case 1:
      return paramRectangle.height;
    case 0:
      return paramRectangle.width;
    }
    throw new IllegalArgumentException("Invalid orientation: " + paramInt1);
  }

  public boolean getScrollableTracksViewportWidth()
  {
    return ((getParent() instanceof JViewport) && (getParent().getWidth() > getPreferredSize().width));
  }

  public boolean getScrollableTracksViewportHeight()
  {
    return ((getParent() instanceof JViewport) && (getParent().getHeight() > getPreferredSize().height));
  }
}